package org.fhkoeln.sgdm.sudoku.logic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class SudokuField
{
	public int [][] values = null;
	public boolean [][] fixed = null;
	
	public SudokuField()
	{
		init();
		
		setStandardSudoku();
		createFixedMap();
	}
	
	public SudokuField(SudokuField source)
	{
		init();
		
		for(int i = 0; i < 9; i ++)
		{
			for(int j = 0; j < 9; j ++)
			{
				this.values[j][i] = source.values[j][i];
				this.fixed[j][i] = source.fixed[j][i];
			}
		}
	}
	
	private void init()
	{
		values = new int[9][9];
		fixed = new boolean[9][9];
	}
	
	public void loadSudoku(File file) throws Exception
	{
		init();
		
		StringBuffer contents = new StringBuffer();
		BufferedReader reader = null;
		 
		reader = new BufferedReader(new FileReader(file));
		String text = null;
		
		int j = 0;
		 
		// repeat until all lines is read
		while ((text = reader.readLine()) != null 
				&& j < 9)
		{
			for(int i = 0; i < 9; i ++)
			{
				values[j][i] = Integer.parseInt("" + text.charAt(i));
			}
			j ++;
		}
		
		createFixedMap();
	}
	
	public String toString()
	{
		String out = "";
		for(int j = 0; j < 9; j ++)
		{
			for(int i = 0; i < 9; i ++)
			{
				out += "" + values[j][i] + ";"; 
			}
			out += "\n";
		}
		return out;
	}
	
	
	private void setStandardSudoku()
	{
		values[0][0] = 0;
		values[0][1] = 3;
		values[0][2] = 2;
		values[0][3] = 0;
		values[0][4] = 8;
		values[0][5] = 0;
		values[0][6] = 0;
		values[0][7] = 0;
		values[0][8] = 5;
		
		values[1][0] = 4;
		values[1][1] = 0;
		values[1][2] = 0;
		values[1][3] = 5;
		values[1][4] = 0;
		values[1][5] = 0;
		values[1][6] = 2;
		values[1][7] = 0;
		values[1][8] = 0;
		
		values[2][0] = 5;
		values[2][1] = 0;
		values[2][2] = 0;
		values[2][3] = 2;
		values[2][4] = 0;
		values[2][5] = 3;
		values[2][6] = 0;
		values[2][7] = 7;
		values[2][8] = 0;
		
		values[3][0] = 8;
		values[3][1] = 6;
		values[3][2] = 4;
		values[3][3] = 0;
		values[3][4] = 5;
		values[3][5] = 2;
		values[3][6] = 7;
		values[3][7] = 9;
		values[3][8] = 1;
		
		values[4][0] = 0;
		values[4][1] = 0;
		values[4][2] = 1;
		values[4][3] = 0;
		values[4][4] = 0;
		values[4][5] = 0;
		values[4][6] = 3;
		values[4][7] = 0;
		values[4][8] = 4;
		
		values[5][0] = 2;
		values[5][1] = 0;
		values[5][2] = 3;
		values[5][3] = 0;
		values[5][4] = 7;
		values[5][5] = 0;
		values[5][6] = 6;
		values[5][7] = 5;
		values[5][8] = 0;
		
		values[6][0] = 0;
		values[6][1] = 4;
		values[6][2] = 5;
		values[6][3] = 0;
		values[6][4] = 0;
		values[6][5] = 0;
		values[6][6] = 1;
		values[6][7] = 8;
		values[6][8] = 0;
		
		values[7][0] = 0;
		values[7][1] = 0;
		values[7][2] = 0;
		values[7][3] = 8;
		values[7][4] = 0;
		values[7][5] = 5;
		values[7][6] = 0;
		values[7][7] = 3;
		values[7][8] = 0;
		
		values[8][0] = 3;
		values[8][1] = 0;
		values[8][2] = 8;
		values[8][3] = 4;
		values[8][4] = 0;
		values[8][5] = 9;
		values[8][6] = 5;
		values[8][7] = 0;
		values[8][8] = 7;
	}
	
	public void createFixedMap()
	{
		for(int j = 0; j < 9; j ++)
		{
			for(int i = 0; i < 9; i ++)
			{
				if(values[j][i] != 0)
				{
					fixed[j][i] = true;
				}
				else
				{
					fixed[j][i] = false;
				}
			}
		}
	}

}
